<?php

class DbmTableInfo extends DbmTable {
	
	/**
	 * 修改次数
	 * @var int
	 */
	public $update_count;
	/**
	 * 最后修改时间
	 * @var datetime
	 */
	public $last_update;
	/**
	 * 最后修改人
	 * @var string
	 */
	public $last_updater;
	
	public $table_name;
	
	public $last_sql;
	
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function attributeLabels() {
		return array_merge(parent::attributeLabels(), array(
			'update_count' => '修改次数',
			'last_update' => '最后修改时间',
			'last_updater' => '最后修改人',
		));
	}
	
	public function rules() {
		return array_merge(parent::rules(), array(
			array('table_name, update_count, last_update, package, last_sql', 'safe'),
		));
	}
	
	public function defaultScope() {
		return array(
			'join' => '
				LEFT JOIN (
					# 最后更新的排在上面
					SELECT * FROM `dbm_history` ORDER BY create_time DESC
				) AS history 
				ON t.table_name = history.table_name
			',
			'select' => '
				t.*,
				COUNT(t.table_name) AS update_count, 
				MAX(history.create_time) AS last_update,
				history.`author` AS last_updater,
				history.sql AS last_sql
			',
			'order' => 't.table_name DESC',
			'group' => 't.table_name',
		);
	}
	
	public function search() {
		
		$criteria = new CDbCriteria();
		$criteria->compare('t.table_name', $this->table_name, true);
		$criteria->compare('t.package', $this->package, true);
		
		return new CActiveDataProvider(__CLASS__, array(
			'criteria' => $criteria,
			'pagination' => array(
				'pageSize' => 20,
			),
		));
	}
	
}

?>